package com.cx.cxapi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.jws.JwsSerialPort;
import com.jws.thirdlibrary.CxManager;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class UartActivity extends Activity implements OnClickListener{
	private static final String TAG = "UartActivity";
	private Button open_uart,write_uart,read_uart;
	private Spinner uartlist ,uart_baudrate;
	private ArrayAdapter<String> uartlistAdapter;
	private static List<String> uartlistContries;

	private CxManager mCxManager;
	private JwsSerialPort mSerialPort=null;

	private OutputStream mOutputStream;
	private InputStream mInputStream;
	private boolean isStop = false;

	private EditText edit_uart_value;
	private TextView read_uart_value;
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.uart);
		open_uart = (Button)findViewById(R.id.open_uart);
		write_uart = (Button)findViewById(R.id.write_uart);
		read_uart = (Button)findViewById(R.id.read_uart);
		edit_uart_value = (EditText)findViewById(R.id.edit_uart_value);
		read_uart_value = (TextView)findViewById(R.id.read_uart_value);		
		open_uart.setOnClickListener(this);
		write_uart.setOnClickListener(this);
		read_uart.setOnClickListener(this);

		write_uart.setEnabled(false);
		read_uart.setEnabled(false);
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		mCxManager = CxManager.create(this);
		String[] SerialPorts=mCxManager.jwsGetSerialPorts();

		Log.i(TAG, "onCreate: SerialPorts.length "+SerialPorts.length);
		for (String value:SerialPorts) {
			Log.i(TAG, "onCreate: value "+value);
		}
		uartlist=(Spinner)findViewById(R.id.uartlist);
		uart_baudrate=(Spinner)findViewById(R.id.uart_baudrate);
		uartlistContries=new ArrayList<String>();
		for(int i=0; i<SerialPorts.length; i++){
			uartlistContries.add(SerialPorts[i]);
		}
		uartlistAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, uartlistContries);
		uartlistAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		uartlist.setAdapter(uartlistAdapter);
	}

	@Override
	public void onClick(View v) {
		
		switch(v.getId()){		
		case R.id.open_uart:
			if (mSerialPort != null) {
				mSerialPort.close();
				mSerialPort=null;
				write_uart.setEnabled(false);
				read_uart.setEnabled(false);
			}
			int Item=uartlist.getSelectedItemPosition();
			String uart = uartlist.getItemAtPosition(Item).toString();
			int Itembaud=uart_baudrate.getSelectedItemPosition();
			String baudrate = uart_baudrate.getItemAtPosition(Itembaud).toString();
			mSerialPort = mCxManager.jwsOpenSerialPort(uart,Integer.parseInt(baudrate));
			if(mSerialPort!=null)
			{
				mOutputStream = mSerialPort.getOutputStream();
				mInputStream = mSerialPort.getInputStream();
				write_uart.setEnabled(true);
				read_uart.setEnabled(true);
				Toast.makeText(getApplicationContext(), "open SerialPort success", Toast.LENGTH_SHORT).show();
			}else
			{
				Toast.makeText(getApplicationContext(), "open SerialPort fail", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.write_uart:
			String text = edit_uart_value.getText().toString();
            writeSerialPort(text);
			break;
		case R.id.read_uart:
			String str=readSerialPort();
			read_uart_value.setText(str);
			break;
		default:
			break;
		}
	}

	public boolean writeSerialPort(String cmd) 
	{
		boolean result = true;
		byte[] mBuffer = cmd.getBytes();
		try {
			if (mOutputStream != null) {
				mOutputStream.write(mBuffer);
			} else {
				result = false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}
	public String readSerialPort() 
	{
		String str=null;
		int size;
		try {
			if (mInputStream == null)
				return null;
			byte[] buffer = new byte[1024];
			size = mInputStream.read(buffer);
			if (size > 0) {
				str=new String(buffer,0,size);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return str;
	}
	
	@Override
    public void onDestroy() {
       if (mSerialPort != null) {
			mSerialPort.close();
			mSerialPort=null;
			write_uart.setEnabled(false);
			read_uart.setEnabled(false);
		}
        super.onDestroy();
    }

}
